package com.xms.form.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.form.api.model.SetFormDefaultStateModel;
import com.xms.form.api.model.UpdateFormModel;
import com.xms.form.entity.SystemForm;
import com.xms.form.service.ISystemFormFinderService;
import com.xms.form.service.ISystemFormUpdaterService;
import com.xms.utils.CollectionUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import com.xms.web.framework.model.SetRecordStateModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * <p>
 * SystemFormUpdaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/18
 */
@RestController
@RequestMapping("{org}/api/schema/form")
public class SystemFormUpdaterController extends ApiBaseController {
    private ISystemFormUpdaterService _systemFormUpdaterService;
    private ISystemFormFinderService _systemFormFinderService;

    @Autowired
    public void SystemFormUpdaterController(ISystemFormUpdaterService systemFormUpdaterService,
                                            ISystemFormFinderService systemFormFinderService) {
        _systemFormUpdaterService = systemFormUpdaterService;
        _systemFormFinderService = systemFormFinderService;
    }

    @ApiOperation("更新表单")
    @PostMapping("update")
    public JsonResultObject update(@RequestBody UpdateFormModel model) throws XmsException, SQLException {
        if (UUIDUtil.isEmpty(model.systemformid)) {
            return JNotSpecifiedParams("systemformid");
        }
        SystemForm form = _systemFormFinderService.getById(model.systemformid);
        if (form == null) {
            return JNotFound("systemformid=" + model.systemformid);
        }
        form.setIscustombutton(model.iscustombutton);
        form.setCustombuttons(model.custombuttons);
        form.setDescription(model.description);
        form.setFormconfig(model.formconfig);
        form.setName(model.name);
        form.setModifiedby(_currentUser.getUser().SystemUserId);
        form.setModifiedon(LocalDateTime.now());
        form.setOrganizationid(_currentUser.getUser().OrganizationId);
        if (_systemFormUpdaterService.update(form)) {
            return JUpdateSuccess();
        }
        return JUpdateFailure();
    }

    @ApiOperation("设置表单可用状态")
    @PostMapping("setstate")
    public JsonResultObject setState(@RequestBody SetRecordStateModel model) {
        if (CollectionUtil.isEmpty(model.recordid)) {
            return JNotSpecifiedParams("id");
        }
        if (_systemFormUpdaterService.updateState(model.isenabled, model.recordid)) {
            return JUpdateSuccess();
        }
        return JUpdateFailure();
    }

    @ApiOperation("设置默认表单")
    @PostMapping("setdefault")
    public JsonResultObject setDefault(@RequestBody SetFormDefaultStateModel model) {
        if (CollectionUtil.isEmpty(model.recordId) || CollectionUtil.isEmpty(model.entityId)) {
            return JNotSpecifiedParams("id", "entityid");
        }
        if (_systemFormUpdaterService.updateDefault(model.entityId, model.recordId[0])) {
            return JUpdateSuccess();
        }
        return JUpdateFailure();
    }
}
